//
//  MapViewController.m
//  PrivateSteward
//
//  Created by lanou3g on 16/3/23.
//  Copyright © 2016年 闫贺晨. All rights reserved.
//

#import "MapViewController.h"
#import <MAMapKit/MAMapKit.h>
#import "RESideMenu.h"
#import <LocalAuthentication/LocalAuthentication.h>
#import <CoreLocation/CoreLocation.h>
@interface MapViewController ()<MAMapViewDelegate>
@property (nonatomic,strong)MAMapView *mapView;
//设置跟随方式的按钮
@property (nonatomic,strong)UIButton *flowStyleButton;
//设置地图类型的按钮
@property (nonatomic,strong)UIButton *mapStyleButton;

@property (nonatomic,strong)UIView *mapStyleView;
//卫星地图
@property (nonatomic,strong)UIButton *statelliteMapStyleButton;
//公交地图
@property (nonatomic,strong)UIButton *busMapStyleButton;
//标准地图
@property (nonatomic,strong)UIButton *standardMapStyleButton;

@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.navigationController.navigationBar.translucent = YES;
    
    
    [MAMapServices sharedServices].apiKey = @"487e80a52ef27f3621c315d1aba12eb0";
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 10)];
    [self.view addSubview:self.mapView];
    

    self.navigationController.navigationBar.translucent = NO;
    //是否开启定位
    self.mapView.showsUserLocation = YES;
    //重写精度圈
    self.mapView.customizeUserLocationAccuracyCircleRepresentation = NO;
    //设置跟随方式
    self.mapView.userTrackingMode = MAUserTrackingModeFollow;
    
//    [self.mapView setZoomLevel:1 animated:YES];

    self.mapView.delegate = self;
    //通过名称获得具体地址
//    CLGeocoder * geocoder = [[CLGeocoder alloc]init];
//    [geocoder geocodeAddressString:@"北京市长城" completionHandler:^(NSArray *placemarks, NSError *error) {
//        
//        NSLog(@"%@",placemarks);
//    }];
    self.flowStyleButton = [[UIButton alloc] initWithFrame:CGRectMake(20, [UIScreen mainScreen].bounds.size.height - 170, 40, 40)];
    self.mapStyleButton = [[UIButton alloc] initWithFrame:CGRectMake(80, [UIScreen mainScreen].bounds.size.height - 170, 40, 40)];
    self.mapStyleView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 240, [UIScreen mainScreen].bounds.size.width - 40, 70)];
    self.statelliteMapStyleButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 10, CGRectGetMaxX(self.mapStyleView.frame)/3, 50)];
    self.busMapStyleButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.mapStyleView.frame)/3, 10, CGRectGetMaxX(self.mapStyleView.frame)/3, 50)];
    self.standardMapStyleButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.mapStyleView.frame)/3*2, 10, CGRectGetMaxX(self.mapStyleView.frame)/3, 50)];
    
    
    self.flowStyleButton.backgroundColor = [UIColor whiteColor];
    self.mapStyleButton.backgroundColor = [UIColor whiteColor];
    self.mapStyleView.backgroundColor = [UIColor whiteColor];
    [self.standardMapStyleButton setImage:[UIImage imageNamed:@"default_main_map_2d_highlighted@2x"] forState:(UIControlStateNormal)];
    [self.statelliteMapStyleButton setImage:[UIImage imageNamed:@"default_main_layer_satelite_normal@2x"] forState:(UIControlStateNormal)];
    [self.busMapStyleButton setImage:[UIImage imageNamed:@"default_main_map_3d_normal@2x"] forState:(UIControlStateNormal)];
    [self.mapStyleButton setImage:[UIImage imageNamed:@"default_main_layer_btn_normal@2x"] forState:(UIControlStateNormal)];
    
    
    
    [self.flowStyleButton addTarget:self action:@selector(changflowStuleAction) forControlEvents:(UIControlEventTouchDown)];
    [self.mapStyleButton addTarget:self action:@selector(changMapStyleAction) forControlEvents:(UIControlEventTouchDown)];
    [self.statelliteMapStyleButton addTarget:self action:@selector(statelliteMapStyleButtonAction) forControlEvents:(UIControlEventTouchDown)];
    [self.standardMapStyleButton addTarget:self action:@selector(standardMapStyleButtonAction) forControlEvents:(UIControlEventTouchDown)];
    [self.busMapStyleButton addTarget:self action:@selector(busMapStyleButtonAction) forControlEvents:(UIControlEventTouchDown)];
    
    
    
    
    [self.mapView addSubview:self.flowStyleButton];
    [self.mapView addSubview:self.mapStyleButton];
    [self.mapView addSubview:self.mapStyleView];
    [self.mapStyleView addSubview:self.standardMapStyleButton];
    [self.mapStyleView addSubview:self.statelliteMapStyleButton];
    [self.mapStyleView addSubview:self.busMapStyleButton];
    
    self.mapStyleView.hidden = YES;
    
    //添加监听 监听跟随状态
    [self.mapView addObserver:self forKeyPath:@"userTrackingMode" options:(NSKeyValueObservingOptionNew) context:nil];

    [self.flowStyleButton setImage:[UIImage imageNamed:@"default_feedback_radio_highlighted@2x"] forState:(UIControlStateNormal)];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapTapAction)];
//    [self.mapView addGestureRecognizer:tap];
    
        
    
}
//修改跟随状态方法
- (void)changflowStuleAction{

    switch (self.mapView.userTrackingMode) {
        case MAUserTrackingModeFollowWithHeading:{
            self.mapView.userTrackingMode = MAUserTrackingModeFollow;
            break;
        }
        case MAUserTrackingModeFollow:{
            self.mapView.userTrackingMode = MAUserTrackingModeFollowWithHeading;
            break;
        }
        default:{
            self.mapView.userTrackingMode = MAUserTrackingModeFollow;
            break;
        }
            
    }
    
}
//修改地图类型的方法
- (void)changMapStyleAction{
    self.mapStyleView.hidden = !self.mapStyleView.hidden;
    if (self.mapStyleView.hidden == YES) {
        [self.mapStyleButton setImage:[UIImage imageNamed:@"default_main_layer_btn_normal@2x"] forState:(UIControlStateNormal)];
    }else{
        [self.mapStyleButton setImage:[UIImage imageNamed:@"default_common_navibar_close_highlighted@3x"] forState:(UIControlStateNormal)];
    }
    
}
- (void)mapTapAction{
    self.mapStyleView.hidden = YES;
    
}
//卫星地图方法
- (void)statelliteMapStyleButtonAction{
    [self.statelliteMapStyleButton setImage:[UIImage imageNamed:@"default_main_layer_satelite_highlighted@2x"] forState:(UIControlStateNormal)];
    [self.standardMapStyleButton setImage:[UIImage imageNamed:@"default_main_map_2d_normal@2x"] forState:(UIControlStateNormal)];
    
    
    self.mapView.mapType = MAMapTypeSatellite;
    self.mapStyleView.hidden = YES;
    [self.mapStyleButton setImage:[UIImage imageNamed:@"default_main_layer_btn_normal@2x"] forState:(UIControlStateNormal)];
}
//标准地图方法
- (void)standardMapStyleButtonAction{
    [self.standardMapStyleButton setImage:[UIImage imageNamed:@"default_main_map_2d_highlighted@2x"] forState:(UIControlStateNormal)];
    
    [self.statelliteMapStyleButton setImage:[UIImage imageNamed:@"default_main_layer_satelite_normal@2x"] forState:(UIControlStateNormal)];
    
    self.mapView.mapType = MAMapTypeStandard;
    self.mapStyleView.hidden = YES;
    [self.mapStyleButton setImage:[UIImage imageNamed:@"default_main_layer_btn_normal@2x"] forState:(UIControlStateNormal)];
}
//公交地图方法
- (void)busMapStyleButtonAction{
    if (self.mapView.showTraffic == YES) {
        [self.busMapStyleButton setImage:[UIImage imageNamed:@"default_main_map_3d_normal@2x"] forState:(UIControlStateNormal)];

    }else{
        [self.busMapStyleButton setImage:[UIImage imageNamed:@"default_main_map_3d_highlighted@2x"] forState:(UIControlStateNormal)];
    }
    
    self.mapStyleView.hidden = YES;
    //对是否显示公交路线 取反
    self.mapView.showTraffic = !self.mapView.showTraffic;
    [self.mapStyleButton setImage:[UIImage imageNamed:@"default_main_layer_btn_normal@2x"] forState:(UIControlStateNormal)];
}
//监听实现的方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"userTrackingMode"]) {
        NSLog(@"新的跟随状态%@",change[@"new"]);
        switch (self.mapView.userTrackingMode) {
            case 0:{
                [self.flowStyleButton setImage:[UIImage imageNamed:@"default_main_gpsnormalbutton_image_normal@2x"] forState:(UIControlStateNormal)];
                break;
            }
            case 1:{
                [self.flowStyleButton setImage:[UIImage imageNamed:@"default_feedback_radio_highlighted@2x"] forState:(UIControlStateNormal)];
                break;
            }
            default:{
                [self.flowStyleButton setImage:[UIImage imageNamed:@"default_main_gpsrotatingbutton_image_normal@2x"] forState:(UIControlStateNormal)];
                break;
            }
                
        }

    }
}
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation
{
    if(updatingLocation)
    {
        //取出当前位置的坐标
//        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        CLLocation *location = [[CLLocation alloc] initWithLatitude:userLocation.coordinate.latitude longitude:userLocation.coordinate.longitude];
        //逆地理编码获得当前地址
        [geocoder reverseGeocodeLocation:location  completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            CLPlacemark * placemark = [placemarks firstObject];
            if (placemark.addressDictionary[@"Name"] != nil) {
                userLocation.title = placemark.addressDictionary[@"Name"];
                NSLog(@"%@",userLocation.title);
//                updatingLocation = NO;
            }
            
            
        }];
    }
 
}


//- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
//{
//    /* 自定义定位精度对应的MACircleView. */
//    if (overlay == mapView.userLocationAccuracyCircle)
//    {
//        MACircleRenderer *accuracyCircleRenderer = [[MACircleRenderer alloc] initWithCircle:overlay];
//        
//        accuracyCircleRenderer.lineWidth    = 2.f;
//        accuracyCircleRenderer.strokeColor  = [UIColor lightGrayColor];
//        accuracyCircleRenderer.fillColor    = [UIColor colorWithRed:1 green:0 blue:0 alpha:.3];
//        
//        return accuracyCircleRenderer;
//    }
//    
//    return nil;
//}

//- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
//{
//    /* 自定义userLocation对应的annotationView. */
//    if ([annotation isKindOfClass:[MAUserLocation class]])
//    {
//        static NSString *userLocationStyleReuseIndetifier = @"userLocationStyleReuseIndetifier";
//        MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:userLocationStyleReuseIndetifier];
//        if (annotationView == nil)
//        {
//            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation
//                                                          reuseIdentifier:userLocationStyleReuseIndetifier];
//        }
//        annotationView.image = [UIImage imageNamed:@"default_account_userphoto_default@2x"];
//        
//        return annotationView;
//    }
//    return nil;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
